#ifndef STATUSMODE_H
#define STATUSMODE_H


#include <iostream>
using namespace std;
class Worker;
class State
{
public:
    virtual void doSomeThing(Worker *w) = 0;
};

class Worker {
public:
    Worker();
    int getHour() {
        return m_hour;
    }
    void setHour(int hour) {
        m_hour = hour;
    }
    State* getCurrentState() {
        return m_currstate;
    }
    void setCurrentState(State* state) {
        m_currstate = state;
    }
    void doSomeThing() //
    {
        m_currstate->doSomeThing(this);
    }
private:
    int m_hour;
    State *m_currstate; //对象的当前状态
};

class State1 : public State {
public:
    void doSomeThing(Worker *w); };
class State2 : public State {
public:void doSomeThing(Worker *w); };

void State1::doSomeThing(Worker *w) {
    if (w->getHour() == 7 || w->getHour()==8) {
        cout << "吃早饭" << endl; }
    else {
        delete w->getCurrentState();
        w->setCurrentState(new State2 );
        w->getCurrentState()->doSomeThing(w); //
    }
}

void State2::doSomeThing(Worker *w) {
    if (w->getHour() == 9 || w->getHour()==10) {
        cout << "工作" << endl; }
    else {
        delete w->getCurrentState();
        //状态 2 不满足 要转到状态 3 后者恢复到初始化状态
        w->setCurrentState(new State1); //恢复到当初状态
        cout << "当前时间点:" << w->getHour() << "未知状态" << endl;
    }
}

Worker::Worker() {
    m_currstate = new State1;
}

void main() {
    Worker *w1 = new Worker;
    w1->setHour(7);
    w1->doSomeThing();
    w1->setHour(9);
    w1->doSomeThing();
    delete w1; cout<<"hello..."<<endl;
    system("pause");
    return ;
}
#endif // STATUSMODE_H
